﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CAGFIP.Utils;



namespace CAGFIP.Jogo
{
    public class AlternativaCollection
    {
        #region Fields
        
        Random rng;
        List<Alternativa> list;
        List<int> wrongIdxList;

        List<int> availableIdxList;

        #endregion

        #region Constructors

        public AlternativaCollection(string altCorreta, string[] altIncorretas)
        {
            int i = 0;
            List.Add(new Alternativa(altCorreta, true));
            AvailableIdxList.Add(i);
            
            while (i < Constants.QtdeAlternativasIncorretas)
            {
                List.Add(new Alternativa(altIncorretas[i], false));
                AvailableIdxList.Add(++i);
            }
        }

        #endregion

        #region Properties

        private Random Rng
        {
            get { return rng ?? (rng = new Random()); }
        }

        private List<Alternativa> List
        {
            get { return list ?? (list = new List<Alternativa>(Constants.QtdeAlternativas)); }
        }

        private List<int> AvailableIdxList
        {
            get { return availableIdxList ?? (availableIdxList = new List<int>(Constants.QtdeAlternativas)); }
        }

        public string this [int index]
        {
            get { return List[index].desc; }
        }   

        #endregion
        
        #region Methods

        //retorna indice de alternativa errada aleatoriamente, e indice para selecao automatica
        public int GetWrong(out int idxAutoSelecao)
        {
            int wrongIdx = wrongIdxList[0];

            AvailableIdxList.Remove(wrongIdx);
            wrongIdxList.RemoveAt(0);

            idxAutoSelecao = AvailableIdxList[0];


            return wrongIdx;
        }

        //retorna o indice da alternativa correta
        public int GetRight()
        {
            int i = 0;
            do
            {
                if (List[i].isCorrect)
                    return i;
                i++;
            } while (i < Constants.QtdeAlternativas);
            return i; //nunca cai aqui
        }

        public AlternativaCollection Shuffle()
        {
            Shuffler.Shuffle(List);
            wrongIdxList = new List<int>();
            for (int i = 0; i < Constants.QtdeAlternativas; i++)
                if (!List[i].isCorrect)
                    wrongIdxList.Add(i);

            wrongIdxList.Shuffle();

            return this;
        }

        #endregion

        /* Nested private class for list value */
        private struct Alternativa
        {
            public string desc;
            public bool isCorrect;

            public Alternativa(string desc, bool isCorrect)
            {
                this.desc = desc;
                this.isCorrect = isCorrect;
            }
        }
    }
}
